# 适用场景 通过 cookie 绕过登录
# 个人信息页面时 utf8 但是访问个人信息页面会自动跳转到登录页面
# 但是登录页面的编码不是 utf8
import urllib.request
url = 'https://weibo.com/u/6233937747'

# UA 信息
headers = {
    'Cookie': 'XSRF-TOKEN=7WumQmv1zwI7TFwoHbSrRsXZ; _s_tentry=weibo.com; Apache=2631396510392.4897.1712883991241; SINAGLOBAL=2631396510392.4897.1712883991241; ULV=1712883991310:1:1:1:2631396510392.4897.1712883991241:; SUB=_2A25LHPkqDeRhGeBM6FEY8ynLzzuIHXVoUHTirDV8PUNbmtB-LUikkW9NRPr9ejbGzBhcxmhNeLaSGzNDJZDz0qg4; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhKf4X5WYgrGnnGAFXpYvcU5NHD95Qceoe01KeNS0BNWs4DqcjVi--ciKL2iKy8i--fi-z7iKysi--4i-i8iK.4PEH8SFHFBb-4SbH8SC-4eEHWSBtt; ALF=02_1715476090; WBPSESS=EttnAhU8mxBlJOqEABcquuvmY30pEtY4seEbUwfcSaCsT4H1dhR4xOyIf6cVCepuUZUbdAGNHfBEiztysf6OnJ7fcpHKX3mVreCUQfUOaGNx4yjB6v78e2E3zgTrMtYR2fPdCZprXPMr5SaOlXJQUg==',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
    'Referer': 'https://weibo.com/u/page/follow/6233937747',
    'X-Xsrf-Token': '7WumQmv1zwI7TFwoHbSrRsXZ'
}

# 定义请求
request = urllib.request.Request(url=url, headers=headers)
# 发送请求
response = urllib.request.urlopen(request)

# 编解码
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 339: invalid continuation byte
# 个人信息页面时 utf8 但是访问个人信息页面会自动跳转到登录页面
# 但是登录页面的编码不是 utf8
content = response.read().decode('utf-8')

print(content)

with open('weibo.html', 'w', encoding='utf-8') as f:
    f.write(content)

